<!DOCTYPE html><html lang="zh-CN"><head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>relativeTimeRounding() | Moment.js &#x6587;&#x6863;</title>
  <link rel="stylesheet" href="static/css/style.css">
  <link rel="shortcut icon" href="http://momentjs.cn/static/img/moment-favicon.png">
  <link rel="stylesheet" href="static/css/atom-one-light.min.css">
</head>
<body class="is-moment">
  <div class="hero hero-oneline">
    <div class="hero-centered">
      <h1>relativeTimeRounding()</h1>
    </div>
  </div>
  <div id="api-section-__FILENAME__">
    <div id="content" class="clearfix">
  
      <div id="column1" data-id="__ID__" class="interior">
        <header>
          <!-- <h1>relativeTimeRounding()</h1> -->
          <div id="gtoc">
            <ul>
              <!-- <li>v</li> -->
              <li>
                <a href="index.html#/customization/relative-time-rounding/" name="toc">&#x8FD4;&#x56DE;&#x4E0A;&#x5C42;&#x6587;&#x6863;</a>
              </li>
              <li style="border: none;">
                <a href="javascript:window.open('https://github.com/momentjscn/moment-docs-cn/edit/master/moment/07-customization/14-relative-time-rounding.md');" rel="nofollow" target="_blank">&#x63D0;&#x4EA4;&#x4FEE;&#x6539;</a>
              </li>
              <!-- <li>
                <a href="/search__SEARCH_PARAM__" name="toc">搜索</a>
              </li> -->
            </ul>
          </div>
          <hr>
        </header>

        <div id="biz_item"></div>
  
        <div id="apicontent">
          <div id="content_left">
							<div class="docs-method-signature">
								<pre><code class="language-js">moment.relativeTimeRounding();  // getter
moment.relativeTimeRounding(fn);  // setter
</code></pre>

							</div>
<p><code>duration.humanize</code> &#x5728;&#x5C06;&#x5176;&#x63D0;&#x4F9B;&#x7ED9;&#x8BED;&#x8A00;&#x73AF;&#x5883;&#x4E2D;&#x6307;&#x5B9A;&#x7684; relativeTime &#x683C;&#x5F0F;&#x5B57;&#x7B26;&#x4E32;&#x4E4B;&#x524D;&#xFF0C;&#x4F1A;&#x820D;&#x5165;&#x4E00;&#x4E2A;&#x53EF;&#x80FD;&#x4E3A;&#x53CC;&#x7CBE;&#x5EA6;&#x7684;&#x503C;&#x3002;
&#x8981;&#x63A7;&#x5236;&#x820D;&#x5165;&#xFF0C;&#x5219;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>moment.relativeTimeRounding</code>&#x3002;</p>
<pre><code class="language-javascript">var roundingDefault = moment.relativeTimeRounding();

// &#x5411;&#x4E0B;&#x820D;&#x5165;&#x76F8;&#x5BF9;&#x65F6;&#x95F4;&#x3002;
moment.relativeTimeRounding(Math.floor);

moment.relativeTimeThreshold(&apos;s&apos;, 60);
moment.relativeTimeThreshold(&apos;m&apos;, 60);
moment.relativeTimeThreshold(&apos;h&apos;, 24);
moment.relativeTimeThreshold(&apos;d&apos;, 31);
moment.relativeTimeThreshold(&apos;M&apos;, 12);

var a = moment();
a.subtract({hours: 23, minutes: 59, seconds: 59});
a.toNow()  // == &apos;23 &#x5C0F;&#x65F6;&#x5185;&apos;  &apos;&#x5411;&#x4E0B;&#x820D;&#x5165;&#x5230;&#x6700;&#x8FD1;&#x7684;&#x5C0F;&#x65F6;&apos;

// &#x56DE;&#x9000;&#x5230;&#x9ED8;&#x8BA4;&#x503C;&#x3002;
moment.relativeTimeRounding(roundingDefault);
</code></pre>
<p>&#x751A;&#x81F3;&#x53EF;&#x4EE5;&#x9009;&#x62E9;&#x5B8C;&#x5168;&#x4E0D;&#x820D;&#x5165;&#xFF1A;</p>
<pre><code class="language-javascript">var retainValue = function (value) {
    return value;
};
moment.relativeTimeRounding(retainValue);

var a = moment();
a.subtract({hours: 39});
a.toNow() // == &apos;1.625 &#x5929;&#x5185;&apos;, &apos;&#x5411;&#x4E0B;&#x820D;&#x5165;&#x5230;&#x6700;&#x8FD1;&#x7684;&#x5E74;&#x4EFD;&apos;
</code></pre>
					</div>
          <div id="content_right">
							<div class="docs-method-signature">
								<pre><code class="language-js">moment.relativeTimeRounding();  // getter
moment.relativeTimeRounding(fn);  // setter
</code></pre>

							</div>
<p><code>duration.humanize</code> rounds a possibly double value before supplying it to the relativeTime format string specified in the locale. To control the rounding you can use <code>moment.relativeTimeRounding</code>.</p>
<pre><code class="language-javascript">var roundingDefault = moment.relativeTimeRounding();

// Round relative time evaluation down
moment.relativeTimeRounding(Math.floor);

moment.relativeTimeThreshold(&apos;s&apos;, 60);
moment.relativeTimeThreshold(&apos;m&apos;, 60);
moment.relativeTimeThreshold(&apos;h&apos;, 24);
moment.relativeTimeThreshold(&apos;d&apos;, 31);
moment.relativeTimeThreshold(&apos;M&apos;, 12);

var a = moment();
a.subtract({hours: 23, minutes: 59, seconds: 59});
a.toNow()  // == &apos;in 23 hours&apos;  &apos;Round down towards the nearest hour&apos;

// back to default
moment.relativeTimeRounding(roundingDefault);
</code></pre>
<p>You can even choose to do no rounding at all:</p>
<pre><code class="language-javascript">var retainValue = function (value) {
    return value;
};
moment.relativeTimeRounding(retainValue);

var a = moment();
a.subtract({hours: 39});
a.toNow() // == &apos;in 1.625 days&apos;, &apos;Round down towards the nearest year&apos;
</code></pre>
					</div>
          <div></div>
        </div>
      </div>
    </div>
  </div>
  
  <script src="static/js/highlight.min.js"></script>
  <script src="static/js/javascript.min.js"></script>
  <script>hljs.initHighlightingOnLoad();</script>
  <script>var _hmt = _hmt || []; (function () { var hm = document.createElement("script"); hm.src = "//hm.baidu.com/hm.js?7a7f78c805fbacc7e075d34f153031aa"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })();</script>

  <style>
    .hero-centered {
      margin: 0;
          margin-left: 10px;
    }
#gtoc ul {
  list-style: none;
  margin-left: 0;
  line-height: 1.5rem;
  padding: 0;
}
#gtoc>ul>li {
  display: inline;
  border-right: 1px solid #000;
  margin-right: .4rem;
  padding-right: .4rem;
}
#gtoc>ul>li>a {
  color: #61b2a7;
}
#content_left {
  width: 49%;float: left;border-right: 1px solid #eee;    padding-right: 2%;
}
#content_right {
float: left;width: 48%;margin-left: 3%;
}
#column1 {
  padding: 0 2rem;
}
  </style>


</body></html>